<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/img/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout" type="info">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle" @click="toggleCollapse()">|||</div>
        <el-menu background-color = '#333744' text-color="#fff" active-text-color="#409eef" unique-opened :collapse = 'isCollapse' :collapse-transition = 'false' router :default-active="activePath">
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
              <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="savaNavState()">
              <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      isCollapse: false,
      activePath: '',
      iconsObj: {
        125: 'iconfont icon-people_fill',
        103: 'iconfont icon-yangshi_icon_tongyong_shield',
        101: 'iconfont icon-yangshi_icon_tongyong_cart',
        102: 'iconfont icon-yangshi_icon_tongyong_trips',
        145: 'iconfont icon-yangshi_icon_tongyong_record'
      }
    }
  },
  created() {
    this.getMenuList()
    this.activePath = this.$route.path
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(this.menulist)
    },
    // 切换
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存路由
    savaNavState () {
      this.activePath = this.$route.path
      console.log(this.activePath)
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.toggle {
  background-color: #4A5064;
  color: #fff;
  line-height: 24px;
  text-align: center;
  font-size: 10px;
  letter-spacing: .2em;
  cursor: pointer;
}

.iconfont {
  margin-right: 10px;
}
</style>
